package com.example.testfullcalendar.controller;

import com.example.testfullcalendar.pojo.MyEvent;
import com.example.testfullcalendar.service.IndexService;
import com.example.testfullcalendar.service.impl.IndexServiceImpl;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/")
public class IndexController {

    private final IndexService indexService;
    @Autowired
    public IndexController(IndexService indexService) {
        this.indexService = indexService;
    }

    @GetMapping("index")
    public String toIndexPage() {
        return "index";
    }

    @RequestMapping(value = "addMyEvent", method = RequestMethod.POST)
    @ResponseBody
    public String addMyEvent(MyEvent eventData) {
        indexService.addMyEvent(eventData);
        return new Gson().toJson(eventData);
    }

    @RequestMapping(value = "getAllEventByTime", method = RequestMethod.GET)
    @ResponseBody
    public String getAllEventByTime(String startTime, String endTime) {
        List<MyEvent> eventList = indexService.findAllEventByTime(startTime, endTime);
        return new Gson().toJson(eventList);
    }

    @RequestMapping(value = "updateEventById", method = RequestMethod.GET)
    @ResponseBody
    public String modifyEventById(MyEvent myEvent) {
        indexService.modifyEventById(myEvent);
        HashMap<String, String> map = new HashMap<>();
        map.put("msg", "success");
        return new Gson().toJson(map);
    }

    @RequestMapping(value = "deleteEventById", method = RequestMethod.GET)
    @ResponseBody
    public String removeEventById(MyEvent myEvent) {
        indexService.removeEventById(myEvent);
        HashMap<String, String> map = new HashMap<>();
        map.put("msg", "success");
        return new Gson().toJson(map);
    }
}
